//
//  UIView+Category.m
//  Platform
//
//  Created by 张宁宁 on 16/9/22.
//  Copyright © 2016年 lirenkj. All rights reserved.
//

#import "UIView+Category.h"

@implementation UIView (Category)

-(void)setViewX:(CGFloat)x{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

-(CGFloat)getViewX{
    
    return self.frame.origin.x;
}

-(void)setViewY:(CGFloat)y{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

-(CGFloat)getViewY{
    
    return self.frame.origin.y;
    
}

-(void)setViewWidth:(CGFloat)width{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

-(CGFloat)getViewWidth{
    
    return self.frame.size.width;
    
}

-(void)setViewHeight:(CGFloat)height{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

-(CGFloat)getViewHeight{
    return self.frame.size.height;
    
}

-(CGFloat)getViewXWidth{
    
    return self.frame.origin.x+self.frame.size.width;
}

-(CGFloat)getViewYHeight{
    
    return self.frame.origin.y+self.frame.size.height;
    
}

-(CGFloat)getCenterY{
    
    return self.center.y;
}


-(CGFloat)getCenterX{
    return self.center.x;
    
}

- (CGFloat)getMaxX
{
    return CGRectGetMaxX(self.frame);
}

- (CGFloat)getMaxY
{
    
    return CGRectGetMaxY(self.frame);
}

-(void)setRoundedCorners:(CGFloat)cornerRadius andBorderWidth:(CGFloat)width andBorderColor:(UIColor*)color{
    [self.layer setCornerRadius:cornerRadius];
    [self.layer setBorderWidth:width];
    [self.layer setBorderColor:color.CGColor];
    [self.layer setMasksToBounds:YES];
}

-(void)setRoundedCornerWithRoundingCorners:(UIRectCorner)corners andcornerRadii:(CGSize)size{
    UIBezierPath * maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                         byRoundingCorners:corners
                                               cornerRadii:size];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    // Set the newly created shape layer as the mask for the image view's layer
    self.layer.mask = maskLayer;
    [self.layer setMasksToBounds:YES];
}

+(CGFloat)getSuitableLength:(CGFloat)length{
    CGFloat scale = ([UIScreen mainScreen].bounds.size.width / 320);
    return length*scale;
}

-(void)setRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)size{

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:size];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;

}


- (void)setX:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (CGFloat)x
{
    return self.frame.origin.x;
}

- (void)setY:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)y
{
    return self.frame.origin.y;
}

- (void)setCenterX:(CGFloat)centerX
{
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (CGFloat)centerX
{
    return self.center.x;
}

- (void)setCenterY:(CGFloat)centerY
{
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

- (CGFloat)centerY
{
    return self.center.y;
}

- (void)setWidth:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (CGFloat)width
{
    return self.frame.size.width;
}

- (void)setHeight:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)height
{
    return self.frame.size.height;
}

- (void)setSize:(CGSize)size
{
    //    self.width = size.width;
    //    self.height = size.height;
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (CGSize)size
{
    return self.frame.size;
}
- (void)setMaxX:(CGFloat)maxX
{
    
}
- (CGFloat)maxX
{
    return self.x + self.width;
}
- (void)setMaxY:(CGFloat)maxY
{
    
}
- (CGFloat)maxY
{
    return self.y + self.height;
}

// 登录后淡入淡出更换rootViewController
+ (void)restoreRootViewController:(UIViewController *)rootViewController{
    
    typedef void (^Animation)(void);
    UIWindow* window = [UIApplication sharedApplication].keyWindow;
    
    rootViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    Animation animation = ^{
        BOOL oldState = [UIView areAnimationsEnabled];
        [UIView setAnimationsEnabled:NO];
        [UIApplication sharedApplication].keyWindow.rootViewController = rootViewController;
        [UIView setAnimationsEnabled:oldState];
    };
    
    [UIView transitionWithView:window
                      duration:0.5f
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:animation
                    completion:nil];
}
@end
